<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function ajout($table)
	{
		if (habilitation())
		{
			$message = '';
			$champs = champs();
			foreach ($champs as $champ)
			{
				$val = $_POST[$champ["id"]];
				if ($champ["obligatoire"] && !strlen($val))
					$message .= 'Le champ <strong>' . trad($champ["Affichage"], APPLNG) . '</strong> est vide mais obligatoire.<br />';
			}
			
			if (strlen($message))
			{
				$retour["statut"] = false;
				$retour["message"] = $message;
			}
			else
			{
				$gp_fonc = GP_fonctions::getInstance();
				$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
				GP_ActiveRecord::setDefaultDBConnection($gp_db);
				
				$now = date("Y-m-d H:i:s");
				$add = new GP_ActiveRecord($table);
				$add -> datecrea = $now;
				$add -> datemodif = $now;
				foreach ($champs as $champ)
				{
					$val = $_POST[$champ["id"]];
					if ($champ["type"] == 'password')
						$val = md5($val);
					$add -> $champ["id"] = $val;
				}
				
				$add -> soumettre();
				
				$retour["statut"] = true;
			}
			return $retour;
		}
		return false;
	}
	
	function champs()
	{
		$retour = array();
		
		$retour['firstname']['id'] = 'firstname';
		$retour['firstname']['Affichage'] = 'Prénom';
		$retour['firstname']['max'] = 255;
		$retour['firstname']['type'] = 'input';
		$retour['firstname']['obligatoire'] = true;
		
		$retour['surname']['id'] = 'surname';
		$retour['surname']['Affichage'] = 'Nom';
		$retour['surname']['max'] = 255;
		$retour['surname']['type'] = 'input';
		$retour['surname']['obligatoire'] = true;
		
		$retour['address']['id'] = 'address';
		$retour['address']['Affichage'] = 'Adresse';
		$retour['address']['max'] = 255;
		$retour['address']['type'] = 'input';
		$retour['address']['obligatoire'] = true;
		
		$retour['address_2']['id'] = 'address_2';
		$retour['address_2']['Affichage'] = 'Adresse (suite)';
		$retour['address_2']['max'] = 255;
		$retour['address_2']['type'] = 'input';
		$retour['address_2']['obligatoire'] = false;
		
		$retour['zip']['id'] = 'zip';
		$retour['zip']['Affichage'] = 'Code postal';
		$retour['zip']['max'] = 255;
		$retour['zip']['type'] = 'input';
		$retour['zip']['obligatoire'] = true;
		
		$retour['city']['id'] = 'city';
		$retour['city']['Affichage'] = 'Ville';
		$retour['city']['max'] = 255;
		$retour['city']['type'] = 'input';
		$retour['city']['obligatoire'] = true;
		
		$retour['country']['id'] = 'country';
		$retour['country']['Affichage'] = 'Pays';
		$retour['country']['max'] = 255;
		$retour['country']['type'] = 'input';
		$retour['country']['obligatoire'] = true;
		
		$retour['email']['id'] = 'email';
		$retour['email']['Affichage'] = 'Courriel';
		$retour['email']['max'] = 255;
		$retour['email']['type'] = 'input';
		$retour['email']['obligatoire'] = true;
		
		$retour['birthdate']['id'] = 'birthdate';
		$retour['birthdate']['Affichage'] = 'Date de naissance';
		$retour['birthdate']['max'] = 11;
		$retour['birthdate']['type'] = 'date';
		$retour['birthdate']['obligatoire'] = true;

		return $retour;
	}
	
	$message_ok = false;
	if ($this -> action_form == 1)
	{
		$form = ajout($this -> table);
		if (!$form["statut"])
		{
			$message_erreur = $form["message"];
			$this -> setvar('message_erreur', $message_erreur);
		}
		else
			$message_ok = true;
	}
	
	$this -> setvar('message_ok', $message_ok);
	$this -> setvar('titre', 'Ajouter un inscrit');
	$this -> setvar('retour_url', rw('mailing_list'));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('champs', champs());
?>